/***************************************************************************
 *   Copyright (C) 2011 by The rPem3d developers                           *
 *   elseifkk@users.sf.net                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef RP3DRECONCLS_H
#define RP3DRECONCLS_H

#include "rp3dRecon.h"
#include "setman.h"

#include <qprocess.h>
#include <qmutex.h>

#include <kconfig.h>

#define MAX2(x, y) ((x) > (y) ? (x) : (y))
#define MIN2(x, y) ((x) > (y) ? (y) : (x))

#define MKHIS_DEF      "mkhisq-dev -c 20 -b 4 -g 4 -y 64 -f"
#define MKTHIS_DEF     "mkthis"
#define MKEHIS_DEF     "mkehis 512 512 8"
#define MKSMAP_DEF     "mkxymaps-dev 20 64"
#define MKRMAP_DEF     "mkxymaps-dev 512 512"
#define MKRWNT_DEF     "mkrawnt 20 64 8"
#define RECON_DEF      "emily -X 20 -g 4 -d 4 -y 64 -vcf"
// #define RECON_DEF      "meliy -x 12 -y 12 -fvc"
#define VIEWER_DEF     "rpview"
#define PLOTTER_DEF    "draw_plot"
#define PLOTSTER_DEF   "draw_plots"
#define ANAPLOTTER_DEF "run_anaplotter"
#define RUNCOM_DEF     "run_com"
#define SCRPATH_DEF    "/usr/local/pem/bin"
#define DATA_DEF       "."
#define FIND_DEF       "/usr/bin/find"
#define XMEDCON_DEF    "/usr/local/bin/xmedcon -mr -f"
#define AMIDE_DEF      "/usr/local/bin/amide"
#define BROWSER_DEF    "/usr/local/bin/firefox"
#define NOTICE_DEF     "/usr/local/pem/bin/kpop -q -1 -n 8"
#define CONFIG         "/.rp3dreconrc"

int const max_num_line = 1000;//<<<<<<<<<<<<<<<<<<<

int const num_recon_mode = 3;
int const RM_EMILY   = 0;
int const RM_MELIY   = 1;
int const RM_UNKNOWN = 2;
unsigned int const hsizes[num_recon_mode]=
{
	( 92*64 ) * ( 92*64 ) *2,
	( 12*12 ) * ( 12*12 ) *20*4,//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	0
};

unsigned int const lmdsizemin=5898240;
char const ext_hst[]=".tmo";
char const ext_dcm[]=".dcm";
char const lbl_hst[]="(H) "; //these should be hdr_hst...
char const lbl_lst[]="[L] ";
char const lbl_nop[]="[X] ";
char const lbl_dcm[]="[d] ";
char const lbl_img[]="[i] ";
char const lbl_plt[]="[p] ";
char const lbl_txt[]="[t] ";
char const lbl_unknown[]="??? ";

int const LBL_SIZE = 1;
int const LBL_HDR = 0;
int const LBL_LASTMODIFY = 2;
int const LBL_LASTMODIFYT = 3;
int const hdrwidth = 4;
int const sizewidth = 7;
int const lastmodifywidth=11;
int const lastmodifytwidth=9; // default not to show

int const FT_HIST  =  0;
int const FT_LIST  =  1;
int const FT_DICM  =  2;
int const FT_NOP   = -1;
int const FT_NENT  = -2;
int const FT_NFLE  = -3;
int const FT_IMAG  =  3;
int const FT_PLOT  =  4;
int const FT_NSUP  =  5;
int const FT_TEXT  =  6;

int const MHO_HISTT=0;
int const MHO_HISTP=1;
int const MHO_HISTD=2;
int const MHO_THIS=3;
int const MHO_SMAP=4;
int const MHO_EHIS=5;
int const MHO_RMAP=6;
int const MHO_RWNT=7;
unsigned int const NREGMAX=1000;

int const fontsizemin=8;
int const fontsizemax=32;

int const WID_MAX = 4;
int const WID_OPERATION=0;
int const WID_SETTINGS =1;
int const WID_FIND     =2;
int const WID_RUN      =3;
int const WID_ETC      =4;

int const NUM_DU = 4;
int const DU_MCI = 0;
int const DU_UCI = 1;
int const DU_MBQ = 2;
int const DU_KBQ = 3;

double const DUFACTOR[NUM_DU]=
{
	3.7e10*10e-3,
	3.7e10*10e-6,
	10e6,
	10e3
};

int const OFMTID_DICOM   = 0;
int const OFMTID_ANALYZE = 1;
int const OFMTID_RAW     = 2;

int const panelblinkinterval=80;

int const nol_max = 4096;//<<<<<<<<<<<<<
int const nel_max = 1024;//<<<<<<<<<<<<<

int const KHC_OPERATOR=0;
int const KHC_BODYPART=1;
int const KHC_RP      =2;
int const KHC_HALFLIFE=3;
int const KHC_OPT     =4;
int const KHC_COM     =5;
int const NUM_KHCS     =6;

class rp3dReconCls: public rp3dReconDlg
{
		Q_OBJECT
	public:
		rp3dReconCls ( QWidget *parent = 0, const char *name = 0 );
		~rp3dReconCls();

	private:
		void countSelectedItems();
		size_t getFileSize ( QString f );
		int getFileType ( QString f, size_t *fs=NULL );
		int getFileType ( unsigned int i, size_t *fs=NULL );
		int setfout ( int ft, QString *f );
		void proc_infs ( QStringList* in, bool onTop=false );
		int check_scr ( QString s, bool quiet=false );
		void check_scr_all ( bool v=false );
		void printmsg ( QString s, QColor c="navy blue", bool bold=false );
		void runscr ( QString s, QString f, bool fullpath=false );
		void doSort ( bool dir );
		QString cfilesize ( size_t s );
		void removeLabelFromDataList ( int c );
		void appendLabelToDataList ( int c );
		bool dirExists ( QString d );
		bool fileExists ( QString f );
		void loadConfig ( QString f );
		void saveConfig ( QString f , int veto=0);
		void proc_outf ( QString f, int ft );
		void appendSize ( int s, QListBox*b );
		void appendHdr ( QListBox*b );
		void appendLastmodify ( int s, QListBox *b );
		void appendLastmodifyt ( int s, QListBox *b );
		void setLabelwidth();
		void removeLbl ( int s, int w, QListBox *b );
		bool str2bool ( QString s );
		void switchControl ( int id );
		void switchButPal ( QPushButton*b,bool on );
		void echoBool ( QString m, int code, bool v=false );
		void setComboItem ( QComboBox *c, int i );
		void setComboItem ( QComboBox *c, QString s );
		void openNormFile ( QString mess, QString f, QLineEdit *e );
		QString outputPath();
		int adjCol ( bool *d, int c, int *off );
		void doTimerEvent(int);
		void resetTimerEvent(int);
		void loadReconConfig(KConfig*,QString);
		void saveReconConfig(KConfig*,QString);
		void setPcc();

	private:
		QProcess *pcm;
		QProcess *pce;
		QProcess *pcv;
		QProcess *pcs;
		QProcess *pcc;

		QTimer *tpcm;
		QTimer *tpce;
		QTimer *tpcs;

		int ecm;
		int ece;
		int ecs;
		int flipm;
		int flipe;
		int flips;

		int numsel;
		int numlf;
		int numhf;
		int numxf;
		int numlft;
		int numhft;
		int numxft;

		bool killedm;
		bool killede;
		bool killeds;

		bool showhdr;
		bool showsize;
		bool showlastmodify;
		bool showlastmodifyt;

		QString lastdir;
		QString lastrdir;
		QString lastsdir;
		QString lastodir;
		QString fouth;
		QString fout;

		int indxh;
		int indxl;

		int mkhisobj;
		bool noupdatebut;
		int pid;
		int uid;
		bool selectMulti;

		QColor bgcolo;
		int labelwidth;

		QString THEOPT;
		QString MKHIS,MKTHIS,MKSMAP,MKEHIS,MKRMAP,MKRWNT,RECON,
			VIEWER,PLOTTER,PLOTSTER,ANAPLOTTER,RUNCOM,NOTICE;
		QString HOME;
		QString USER;
		QString CWD;
		QString SCRPATH;
		QString DATA;
		QString FIND,XMEDCON,AMIDE;

		int currentPage;
		QPushButton *controlButs[5];

		QString webbrowser;
		int currentDoseUnit;

		int recon_mode;

		int timerid;
		QColor theBgCol;

		int ofmtid;

		QString lastimagefile;
		QString lastimagefilefull;
		QString lastoutimagefile;

		int theOffs[3];
		bool theFlips[3];
		bool theOks[3];

		QMutex timerMT;

		int nelc,nelm,nele,nels;
		int nolc,nolm,nole,nols;

		QString iopCode; /* image orientation patient */

		QStringList dinfs;
		QStringList sinfs;
		QMutex dropeventMT;

		int shmid;
		setManCls *setmanw;
		int smwm;
		QString smwn;
		KHistoryCombo *khcs[NUM_KHCS];

	private slots:
		void killeSlot();
		void killmSlot();
		void setfoutSlot();
		void setfhoutSlot();
		void reconSlot();
		void mkhisSlot();
		void selectAllSlot();
		void cleSlot();
		void delSlot();
		void openFileSlot();
		void updateButSlot();
		void slotReadStdoute();
		void slotReadStderre();
		void reconExited();
		void slotReadStdoutm();
		void slotReadStderrm();
		void convExited();
		void pcmSlot();
		void pceSlot();
		void revSlot();
		void showAbout();
		void viewSlot();
		void amideSlot();
		void opendirSlot();
		void viewerDone();
		void regSlot();
		void killsearchSlot();
		void searchSlot();
		void runSearchSlot();
		void slotReadStdouts();
		void slotReadStderrs();
		void searchDone();
		void pcsSlot();
		void viewResultSlot();
		void openResultSlot();
		void canViewSlot();
		void doUpdateButSlot();
		void enableButSlot();
		void selectHSlot();
		void selectLSlot();
		void selectXSlot();
		void sortUpSlot();
		void sortDownSlot();
		void magSlot();
		void redSlot();
		void runComSlot();
		void slotReadStdoutc();
		void slotReadStderrc();
		void comDone();
		void showFileTypeSlot();
		void showFileSizeSlot();
		void showFileLMDSlot();
		void showFileLMTSlot();
		void infRBClickSlot ( QListBoxItem*, const QPoint& );
		void toggleShowFileTypeSlot();
		void toggleShowFileSizeSlot();
		void toggleShowFileLMDSlot();
		void toggleShowFileLMTSlot();
		void openScrpathSlot();
		void openDataDirSlot();
		void datapathSlot();
		void scrpathSlot();
		void infdirSlot();
		void recvDropped ( QDropEvent* );
		void toggleSelectionModeMul();
		void toggleSelectionModeExt();
		void cleLogSlot();
		void wrapLogSlot();
		void operationSlot();
		void settingsSlot();
		void findSlot();
		void runSlot();
		void etcSlot();
		void logSlot();
		void checkScrSlot();
		void lockSlot();
		void stayTopSlot();
		void canCommitSlot();
		void commitInfoSlot();
		void clearInfoSlot();
		void clearInfoSlot(bool all);
		void doseUnitSlot();
		void setAgeSlot();
		void openUniFileSlot();
		void openSenFileSlot();
		void rdManSlot();
		void killComSlot();
		void setOutputFilenameSlot();
		void setOutputFilenameSlot(bool setpath);
		void outfSelected();
		void bodyPartEnterSlot();
		void showHelp();
		void cleHisSlot();
		void setManSlot();
		void loadReconSetting(QString);
		void saveReconSetting(QString);
		void deleReconSetting(QString);
		void oriSlot();

	protected:
		void mouseReleaseEvent ( QMouseEvent * );
		void  closeEvent ( QCloseEvent *event );
		void keyPressEvent ( QKeyEvent *e );
		void dropEvent ( QDropEvent *e );
		void dragEnterEvent ( QDragEnterEvent *e );
		void timerEvent ( QTimerEvent *e );
};

#endif
